/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TrangSuc;

import Func.Conn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;




/**
 *
 * @author URUKU
 */
@WebServlet(name = "XuLyThanhToan", urlPatterns = {"/XuLyThanhToan"})
public class XuLyThanhToan extends HttpServlet {
final private String errorpage   = "ErrorPage.jsp";
    final private String welcomePage = "hoantat.jsp";
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           String action = request.getParameter("btnXacNhan");
            if (action.equals("Next")) {
            /*
             * TODO output your page here. You may use following sample code.
             */
            int MaKH = 0;
            String url = errorpage;
            String HoTen =request.getParameter("TenKH");
            String SDT =request.getParameter("SDT");
            String CMND =request.getParameter("CMND");
            String SoTK =request.getParameter("SoTK");
            String Email =request.getParameter("EMail");
            String NoiNhan =request.getParameter("NoiNhan");
            String TT =request.getParameter("TT");
            Connection con = Conn.GetCon();
            String sql1 = "select makh from khachhang where CMND = '"+CMND+"'";
            Statement stm = con.createStatement();

            // stm.setString(1, username);
            // stm.setString(2, password);
            ResultSet rs = stm.executeQuery(sql1);
            if (rs.next()) {
                MaKH = rs.getInt("MaKH");
            }
            String sql = "insert into DONHANG(MaKH,Thoidiemdathang,TenNguoiNhan,DiadiemGiaoHang,Email,DienThoai,TongTien,TinhTrang,GhiChu) values('"+ MaKH +"','2013-06-19','"+HoTen+"','"+NoiNhan+"','"+Email+"','"+SDT+"','"+TT+"','1','')";         
            Statement stmt = con.createStatement();
            int smt=stmt.executeUpdate(sql);
            if(smt >= 1)
            {   
                url = welcomePage;
            }
            RequestDispatcher rd = request.getRequestDispatcher(url);
               rd.forward(request, response);
           }
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(null, "" + e.getMessage());
        }
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
